<?php


namespace App\Logic;


use App\Interfaces\BillerInterface;
use App\Repository\OrderRepository;
use Carbon\Carbon;
use mysql_xdevapi\Exception;

class OrderProcess
{

    /**
     * OrderProcess constructor.
     */
    public function __construct(BillerInterface $biller, OrderRepository $order, array $validators = array())
    {
        $this->biller = $biller;
        $this->order = $order;
        $this->validators = $validators;
    }

    /***
     * 使用依赖注入，代码可读性增强，也容易增强，如果验证订单的业务规则发生改变，或者说是有了新的规则了呢
     * @param Order $order
     */
    public function process(Order $order)
    {
//        $recent = $this->order->getRecentOrderCount($order->account);
//        if ($recent > 0) {
//            throw new Exception('Suplicatae order likely');
//        }

        foreach ($this->validators as $validator) {
            $this->validators($order);
        }

        $this->biller->bill($order->account->id, $order->amount);

        $this->order->logOrder($order);

    }

//    protected function getRecentOrderCount(Order $order)
//    {
//        $timestamp = Carbon::now()->subMinutes(5);
//        return DB::table('orders')->where('account', $order->account->id)
//            ->where('created_at', '>=', $timestamp)
//            ->count();
//    }

}